class OccasionItemsController < ApplicationController
  before_filter :setup_layout

  #verify :method => :post, :only => [ :create ],
  #       :redirect_to => { :action => :index }

  # GET /occasion_items
  # GET /occasion_items.xml
  def index
    selected_items = OccasionBoardEntry.find(:all, :conditions => ['symbol = ? ', params[:symbol]]) 
    selected_urls = selected_items.map{|item| item.url }
    @occasion_items = OccasionItem.find({:category=>"Baby", :order=>:rank}, selected_urls )
    @occasion = Occasion.find_by_oid(Symbol.split_symbol(params[:symbol]).last)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @occasion_items }
    end
  end

  # GET /occasion_items/1
  # GET /occasion_items/1.xml
  def show
    @occasion_item = OccasionItem.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @occasion_item }
    end
  end

  # GET /occasion_items/new
  # GET /occasion_items/new.xml
  def new
    @occasion_item = OccasionItem.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @occasion_item }
    end
  end

  # GET /occasion_items/1/edit
  def edit
    @occasion_item = OccasionItem.find(params[:id])
  end

  # POST /occasion_items
  # POST /occasion_items.xml
  def create
    @occasion_item = OccasionItem.new(params[:occasion_item])

    respond_to do |format|
      if @occasion_item.save
        flash[:notice] = 'OccasionItem was successfully created.'
        format.html { redirect_to(@occasion_item) }
        format.xml  { render :xml => @occasion_item, :status => :created, :location => @occasion_item }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @occasion_item.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /occasion_items/1
  # PUT /occasion_items/1.xml
  def update
    @occasion_item = OccasionItem.find(params[:id])

    respond_to do |format|
      if @occasion_item.update_attributes(params[:occasion_item])
        flash[:notice] = 'OccasionItem was successfully updated.'
        format.html { redirect_to(@occasion_item) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @occasion_item.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /occasion_items/1
  # DELETE /occasion_items/1.xml
  def destroy
    @occasion_item = OccasionItem.find(params[:id])
    @occasion_item.destroy

    respond_to do |format|
      format.html { redirect_to(occasion_items_url) }
      format.xml  { head :ok }
    end
  end
private
  def setup_layout
    @main_menu = @title = 'お祝いイベント'
    @tab_menu_source = [['お祝いイベントに戻る', 'index']]
  end
end
